﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;
using System.Security.Principal;

namespace Check_Printing_Solution
{
    public class PrintChequeEngine
    {
        Cheque cheque = new Cheque();
        PrintManager manager = new PrintManager();

        int chequesprinted = 0;
        int _chequecount = 0;
        int _coversprinted = 0;
        string _coverpage_nextchequenumber = "";
        int _incrementer = 0;
        int _pagecount = 1;
        int _totalchequesprinted = 0;
        bool _iscopy = false;

        private void Print( TemplateLayout styletemplate, int quantity, string printername )
        {
            if ( cheque.CurrentTemplate.ChquesPerPage == "0" || cheque.CurrentTemplate.ChquesPerPage == "" )
            {
                chequesprinted = quantity;
            }

            for ( int i = 0; i < quantity; i++ )
            {
                //Set standard print controller
                PrintDocument template = new PrintDocument();
                PrintController printcontroller = new StandardPrintController();
                template.PrintController = printcontroller;

                //Create the event handler for printing the cheques
                template.PrintPage += new PrintPageEventHandler( template_PrintPage );
                template.PrinterSettings.PrinterName = printername;
                template.DefaultPageSettings.PaperSize = new PaperSize( "Label Paper", cheque.CurrentTemplate.PaperWidth, cheque.CurrentTemplate.PaperHeight );

                //Assign the paper source from selected printer
                PaperSource papersource = new PaperSource();
                papersource.SourceName = cheque.CurrentTemplate.Tray;
                template.DefaultPageSettings.PaperSource = papersource;

                if ( styletemplate.Landscape )
                {
                    template.DefaultPageSettings.Landscape = true;
                }
                else
                {
                    template.DefaultPageSettings.Landscape = false;
                }

                if ( cheque.CurrentTemplate.NoCopies == 3 )
                {
                    //Cheque
                    template.Print();
                    _chequecount = _chequecount - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    chequesprinted = chequesprinted - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    _pagecount = _pagecount - 1;

                    _iscopy = true;

                    //Copy 1
                    template.Print();
                    _chequecount = _chequecount - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    chequesprinted = chequesprinted - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    _pagecount = _pagecount - 1;

                    //Copy 2
                    template.Print();
                    _chequecount = _chequecount - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    chequesprinted = chequesprinted - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    _pagecount = _pagecount - 1;

                    //Copy 3
                    template.Print();

                    _iscopy = false;
                }
                else if ( cheque.CurrentTemplate.NoCopies == 2 )
                {
                    //Cheque
                    template.Print();
                    _chequecount = _chequecount - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    chequesprinted = chequesprinted - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    _pagecount = _pagecount - 1;

                    _iscopy = true;

                    //Copy 1
                    template.Print();
                    _chequecount = _chequecount - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    chequesprinted = chequesprinted - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    _pagecount = _pagecount - 1;

                    //Copy 2
                    template.Print();

                    _iscopy = false;
                }
                else if ( cheque.CurrentTemplate.NoCopies == 1 )
                {
                    //Cheque
                    template.Print();
                    _chequecount = _chequecount - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    chequesprinted = chequesprinted - Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage );
                    _pagecount = _pagecount - 1;

                    _iscopy = true;

                    //Copy 1
                    template.Print();

                    _iscopy = false;
                }
                else if ( cheque.CurrentTemplate.NoCopies == 0 )
                {
                    _iscopy = false;
                    template.Print();
                }
                else
                {
                    template.Print();
                }
            }
        }

        void template_PrintPage( object sender, PrintPageEventArgs e )
        {
            int padding = 0;
            int chequeincrementer = 0;

            for ( int i = 0; i < Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage ); i++ )
            {
                int sequencenumber = 0;
                FontStyle fntstyle = FontStyle.Regular;

                if ( !cheque.IgnoreSequence )
                {
                    if ( !cheque.CurrentTemplate.Skip )
                    {
                        sequencenumber = ( chequeincrementer + ( _pagecount - 1 ) + cheque.LastChequeNumber );
                    }
                    else
                    {
                        sequencenumber = _totalchequesprinted + cheque.LastChequeNumber;
                    }
                }
                else
                {
                    sequencenumber = Convert.ToInt32( _coverpage_nextchequenumber );
                }

                string checkno = string.Format( "C{0}C A{1}D{2}A {3}C  {4}",
                    FormatNumber( sequencenumber.ToString().Trim(), Convert.ToInt32( cheque.CurrentTemplate.SeqNumberSize ) ),
                    FormatNumber( cheque.CurrentBank.BranchPrefix.Trim(), Convert.ToInt32( cheque.CurrentTemplate.BranchNumberSize ) ),
                    FormatNumber( cheque.CurrentBank.BranchCode.Trim(), Convert.ToInt32( cheque.CurrentTemplate.TransitNumberSize ) ),
                    FormatNumber( cheque.CurrentCustomer.ChequeNumber.Trim(), Convert.ToInt32( cheque.CurrentTemplate.AccountNumberSize ) ),
                    cheque.CurrentTemplate.DepositSlips.Trim() );

                if ( cheque.CurrentTemplate.BoldNameCheque == true )
                {
                    fntstyle = FontStyle.Bold;
                }
                else
                {
                    fntstyle = FontStyle.Regular;
                }

                if ( cheque.CurrentTemplate.PrintNames )
                {
                    e.Graphics.DrawString( cheque.CurrentCustomer.FirstName + " " + cheque.CurrentCustomer.LastName, new Font( cheque.CurrentTemplate.NameFont, Convert.ToSingle( cheque.CurrentTemplate.NameFontSize ), FontStyle.Bold ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.NameX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.NameY ) + cheque.OffsetY + padding ) );
                }
                if ( cheque.CurrentTemplate.PrintNameAddress )
                {
                    //Line 1
                    if ( cheque.CurrentTemplate.ItalicLine1Cheque )
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line1, new Font( cheque.CurrentTemplate.Address1Font, Convert.ToSingle( cheque.CurrentTemplate.Address1FontSize ), fntstyle | FontStyle.Italic ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address1X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address1Y ) + cheque.OffsetY + padding ) );
                    }
                    else
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line1, new Font( cheque.CurrentTemplate.Address1Font, Convert.ToSingle( cheque.CurrentTemplate.Address1FontSize ), fntstyle ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address1X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address1Y ) + cheque.OffsetY + padding ) );
                    }
                    //Line 2
                    if ( cheque.CurrentTemplate.ItalicLine2Cheque )
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line2, new Font( cheque.CurrentTemplate.Address2Font, Convert.ToSingle( cheque.CurrentTemplate.Address2FontSize ), FontStyle.Italic ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address2X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address2Y ) + cheque.OffsetY + padding ) );
                    }
                    else
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line2, new Font( cheque.CurrentTemplate.Address2Font, Convert.ToSingle( cheque.CurrentTemplate.Address2FontSize ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address2X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address2Y ) + cheque.OffsetY + padding ) );
                    }
                }
                if ( cheque.CurrentTemplate.PrintChequeNos )
                {
                    FontFamily font = new FontFamily( cheque.CurrentTemplate.ChequeNumberFont );
                    e.Graphics.DrawString( checkno, new Font( font, Convert.ToSingle( cheque.CurrentTemplate.ChequeNumberFontSize ), FontStyle.Regular, GraphicsUnit.Pixel ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.ChequeNumberX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.ChequeNumberY ) + cheque.OffsetY + padding ) );
                }

                if ( cheque.CurrentTemplate.PrintStubNos )
                {
                    e.Graphics.DrawString( FormatNumber( sequencenumber.ToString(), Convert.ToInt32( cheque.CurrentTemplate.SeqNumberSize ) ), new Font( cheque.CurrentTemplate.ChequeCountFont, Convert.ToSingle( cheque.CurrentTemplate.ChequeCountFontSize ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.ChequeCountX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.ChequeCountY ) + cheque.OffsetY + padding ) );
                }

                if ( cheque.CurrentTemplate.PrintStubNos2 )
                {
                    e.Graphics.DrawString( FormatNumber( sequencenumber.ToString(), Convert.ToInt32( cheque.CurrentTemplate.SeqNumberSize ) ), new Font( cheque.CurrentTemplate.StubFont, Convert.ToSingle( cheque.CurrentTemplate.StubSize ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.StubX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.StubY ) + cheque.OffsetY + padding ) );
                }

                //Line 3
                if ( cheque.CurrentTemplate.ItalicLine3Cheque )
                {
                    e.Graphics.DrawString( cheque.CurrentCustomer.Line3, new Font( cheque.CurrentTemplate.Address3Font, Convert.ToSingle( cheque.CurrentTemplate.Address3Size ), FontStyle.Italic ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address3X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address3Y ) + cheque.OffsetY + padding ) );
                }
                else
                {
                    e.Graphics.DrawString( cheque.CurrentCustomer.Line3, new Font( cheque.CurrentTemplate.Address3Font, Convert.ToSingle( cheque.CurrentTemplate.Address3Size ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address3X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address3Y ) + cheque.OffsetY + padding ) );
                }

                if ( cheque.CurrentTemplate.ItalicLine4Cheque )
                {
                    e.Graphics.DrawString( cheque.CurrentCustomer.Line4, new Font( cheque.CurrentTemplate.Address4Font, Convert.ToSingle( cheque.CurrentTemplate.Address4Size ), FontStyle.Italic ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address4X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address4Y ) + cheque.OffsetY + padding ) );
                }
                else
                {
                    e.Graphics.DrawString( cheque.CurrentCustomer.Line4, new Font( cheque.CurrentTemplate.Address4Font, Convert.ToSingle( cheque.CurrentTemplate.Address4Size ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.Address4X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.Address4Y ) + cheque.OffsetY + padding ) );
                }
                if ( cheque.CurrentTemplate.PrintBranchAddress )
                {
                    e.Graphics.DrawString( cheque.CurrentBank.BankAddress, new Font( cheque.CurrentTemplate.BankFont, Convert.ToSingle( cheque.CurrentTemplate.BankFontSize ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.BankX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.BankY ) + cheque.OffsetY + padding ) );
                }
                padding = padding + Convert.ToInt32( cheque.CurrentTemplate.DistanceBetweenCheques );
                _chequecount++;
                chequesprinted++;
                chequeincrementer = chequeincrementer + _incrementer;
                if ( _iscopy == false )
                {
                    _totalchequesprinted++;
                }
            }
            _pagecount++;
        }

        private void PrintCover( string printername )
        {
            chequesprinted = 0;

            PrintDocument template = new PrintDocument();
            PrintController printcontroller = new StandardPrintController();
            template.PrintController = printcontroller;

            template.QueryPageSettings += new QueryPageSettingsEventHandler( template_QueryPageSettings );
            template.PrintPage += new PrintPageEventHandler( covertemplate_PrintPage );
            template.PrinterSettings.PrinterName = printername;
            template.DefaultPageSettings.PaperSize = new PaperSize( "Label Paper", cheque.CurrentTemplate.PaperWidth, cheque.CurrentTemplate.PaperHeight );
            PaperSource papersource = new PaperSource();
            papersource.SourceName = cheque.CurrentTemplate.Tray;
            template.DefaultPageSettings.PaperSource = papersource;

            template.Print();
            if ( cheque.CurrentTemplate.PrintDearCustomer )
            {
                template.Print();
            }
        }

        void template_QueryPageSettings( object sender, QueryPageSettingsEventArgs e )
        {
            if ( cheque.CurrentTemplate.Landscape )
            {
                e.PageSettings.Landscape = true;
            }
            else
            {
                e.PageSettings.Landscape = false;
            }
        }

        void covertemplate_PrintPage( object sender, PrintPageEventArgs e )
        {
            for ( int j = 0; j < cheque.CurrentTemplate.CoverCount; j++ )
            {
                int padding = 0;

                for ( int i = 0; i < Convert.ToInt32( cheque.CurrentTemplate.ChquesPerPage ); i++ )
                {
                    FontStyle fntstyle = FontStyle.Regular;

                    if ( cheque.CurrentTemplate.BoldNameCover == true )
                    {
                        fntstyle = FontStyle.Bold;
                    }
                    else
                    {
                        fntstyle = FontStyle.Regular;
                    }

                    if ( cheque.CurrentTemplate.PrintBranchAddress )
                    {
                        e.Graphics.DrawString( cheque.CurrentBank.BankAddress, new Font( cheque.CurrentTemplate.CoverAddress4Font, Convert.ToSingle( cheque.CurrentTemplate.CoverAddress4Size ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverAddress4X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverAddress4Y ) + cheque.OffsetY + padding ) );
                    }
                    //Name
                    e.Graphics.DrawString( cheque.CurrentCustomer.FirstName + " " + cheque.CurrentCustomer.LastName, new Font( cheque.CurrentTemplate.CoverNameFont, Convert.ToSingle( cheque.CurrentTemplate.CoverNameSize ), FontStyle.Bold ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverNameX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverNameY ) + cheque.OffsetY + padding ) );
                    //Address1
                    if ( cheque.CurrentTemplate.ItalicLine2Cover )
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line2, new Font( cheque.CurrentTemplate.CoverAddress1Font, Convert.ToSingle( cheque.CurrentTemplate.CoverAddress1Size ), FontStyle.Italic ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverAddress1X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverAddress1Y ) + cheque.OffsetY + padding ) );
                    }
                    else
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line2, new Font( cheque.CurrentTemplate.CoverAddress1Font, Convert.ToSingle( cheque.CurrentTemplate.CoverAddress1Size ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverAddress1X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverAddress1Y ) + cheque.OffsetY + padding ) );
                    }
                    //Address2
                    if ( cheque.CurrentTemplate.ItalicLine3Cover )
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line3, new Font( cheque.CurrentTemplate.CoverAddress2Font, Convert.ToSingle( cheque.CurrentTemplate.CoverAddress2Size ), FontStyle.Italic ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverAddress2X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverAddress2Y ) + cheque.OffsetY + padding ) );
                    }
                    else
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line3, new Font( cheque.CurrentTemplate.CoverAddress2Font, Convert.ToSingle( cheque.CurrentTemplate.CoverAddress2Size ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverAddress2X ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverAddress2Y ) + cheque.OffsetY + padding ) );
                    }
                    //Phone
                    if ( cheque.CurrentTemplate.ItalicLine1Cover )
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line1, new Font( cheque.CurrentTemplate.CoverTelephoneFont, Convert.ToSingle( cheque.CurrentTemplate.CoverTelephoneSize ), fntstyle | FontStyle.Italic ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverTelephoneX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverTelephoneY ) + cheque.OffsetY + padding ) );
                    }
                    else
                    {
                        e.Graphics.DrawString( cheque.CurrentCustomer.Line1, new Font( cheque.CurrentTemplate.CoverTelephoneFont, Convert.ToSingle( cheque.CurrentTemplate.CoverTelephoneSize ), fntstyle ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverTelephoneX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverTelephoneY ) + cheque.OffsetY + padding ) );
                    }
                    //Next Cheque #
                    e.Graphics.DrawString( FormatNumber( _coverpage_nextchequenumber, Convert.ToInt32( cheque.CurrentTemplate.SeqNumberSize ) ), new Font( cheque.CurrentTemplate.CoverCheckCountFont, Convert.ToSingle( cheque.CurrentTemplate.CoverCheckCountSize ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverCheckCountX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverCheckCountY ) + cheque.OffsetY + padding ) );

                    //CoverCheckCount
                    string checkno = string.Format( "C{0}C A{1}D{2}A {3}C  {4}",
                    FormatNumber( _coverpage_nextchequenumber.ToString().Trim(), Convert.ToInt32( cheque.CurrentTemplate.SeqNumberSize ) ),
                    FormatNumber( cheque.CurrentBank.BranchPrefix.Trim(), Convert.ToInt32( cheque.CurrentTemplate.BranchNumberSize ) ),
                    FormatNumber( cheque.CurrentBank.BranchCode.Trim(), Convert.ToInt32( cheque.CurrentTemplate.TransitNumberSize ) ),
                    FormatNumber( cheque.CurrentCustomer.ChequeNumber.Trim(), Convert.ToInt32( cheque.CurrentTemplate.AccountNumberSize ) ),
                    cheque.CurrentTemplate.DepositSlips.Trim() );

                    e.Graphics.DrawString( checkno, new Font( cheque.CurrentTemplate.CoverCheckNoFont, Convert.ToSingle( cheque.CurrentTemplate.CoverCheckNoSize ) ), Brushes.Black, new Point( Convert.ToInt32( cheque.CurrentTemplate.CoverCheckNoX ) + cheque.OffsetX, Convert.ToInt32( cheque.CurrentTemplate.CoverCheckNoY ) + cheque.OffsetY + padding ) );

                    padding = padding + Convert.ToInt32( cheque.CurrentTemplate.DistanceBetweenCheques );
                }
                _coversprinted++;
            }
        }

        public void InitiatePrint( string printername, Customer customer, Bank bank, string style,
            int totalchequesforprint, string comment, string lastchequenumber, string styleid, bool ignoreseq, string numberbooks,
            int offsetx, int offsety, bool isreprint )
        {
            cheque.IgnoreSequence = ignoreseq;
            cheque.CurrentCustomer = customer;
            cheque.CurrentBank = bank;
            _coverpage_nextchequenumber = ( Convert.ToInt32( lastchequenumber ) + totalchequesforprint ).ToString();
            cheque.LastChequeNumber = Convert.ToInt32( lastchequenumber );
            cheque.OffsetX = offsetx;
            cheque.OffsetY = offsety;

            string bankcode = bank.BranchPrefix;
            cheque.CurrentBank = manager.GetBankByBankCode( bankcode );

            TemplateLayout template = manager.GetTemplateLayout( style, styleid );
            cheque.CurrentTemplate = template;

            int pagesperbook = 0;
            int leaves = 0;
            int numbooks = 0;
            int remainder = 0;

            pagesperbook = Convert.ToInt32( template.NoChequesPerBook );
            if ( totalchequesforprint >= pagesperbook )
            {
                leaves = totalchequesforprint / Convert.ToInt32( template.ChquesPerPage );
                numbooks = leaves / Convert.ToInt32( template.NoChequesPerBook );
                if ( numbooks == 0 )
                {
                    numbooks = 1;
                }
                remainder = totalchequesforprint - ( numbooks * Convert.ToInt32( template.NoChequesPerBook ) * Convert.ToInt32( template.ChquesPerPage ) );
                if ( remainder < 0 )
                {
                    remainder = 0;
                }
            }
            else if ( pagesperbook > totalchequesforprint )
            {
                remainder = totalchequesforprint;
            }
            else
            {
                numbooks = 1;
            }

            _incrementer = Convert.ToInt32( template.NoChequesPerBook );

            for ( int j = 0; j < numbooks; j++ )
            {
                if ( template.PrintCovers )
                {
                    for ( int i = 0; i < cheque.CurrentTemplate.CoverCount; i++ )
                    {
                        PrintCover( printername );
                    }
                }
                if ( _pagecount != 1 && !template.Skip )
                {
                    _pagecount = _totalchequesprinted + 1;
                }
                int pages = totalchequesforprint / Convert.ToInt32( template.ChquesPerPage );
                Print( template, pages, printername );
                chequesprinted = 0;
                _chequecount = 1;
            }

            if ( remainder > 0 )
            {
                if ( totalchequesforprint < pagesperbook )
                {
                    _incrementer = totalchequesforprint / Convert.ToInt32( template.ChquesPerPage );
                }
                if ( template.PrintCovers )
                {
                    for ( int i = 0; i < cheque.CurrentTemplate.CoverCount; i++ )
                    {
                        PrintCover( printername );
                    }
                }
                if ( _pagecount != 1 && !template.Skip )
                {
                    _pagecount = _totalchequesprinted + 1;
                }
                Print( template, remainder, printername );
                chequesprinted = 0;
                _chequecount = 1;
            }

            CreateJobLog( comment,
                totalchequesforprint.ToString(),
                lastchequenumber,
                customer.ChequeNumber,
                style, bank.BranchPrefix, bank.Name, numbooks.ToString(),
                customer.FirstName + " " + customer.LastName, totalchequesforprint.ToString(), isreprint
                );
        }

        public string FormatNumber( string seqnumber, int templatelength )
        {
            int templaterequired = templatelength;
            int currentlength = seqnumber.ToString().Count();
            int gap = templaterequired - currentlength;
            string holder = "";

            for ( int i = 0; i < gap; i++ )
            {
                holder = holder + "0";
            }
            holder = holder + seqnumber;

            return holder;
        }

        public string FormatNumber( string seqnumber, int templatelength, int fieldlength )
        {
            int templaterequired = templatelength;
            int currentlength = seqnumber.ToString().Count();
            int gap = templaterequired - currentlength;
            string holder = "";

            for ( int i = 0; i < gap; i++ )
            {
                holder = holder + "0";
            }
            holder = holder + seqnumber;

            int fieldgap = fieldlength - holder.Length;
            for ( int i = 0; i < fieldgap; i++ )
            {
                holder = " " + holder;
            }

            return holder;
        }

        private void CreateJobLog( string comment, string quantity, string lastchequenumber, string accountnumber, string chequetype,
            string bank, string branch, string numberbooks, string customername, string numbercheques, bool isreprint )
        {
            JobLog joblog = new JobLog();
            joblog.Comment = comment;
            joblog.DatePrinted = DateTime.Now;
            joblog.LastQuantity = quantity;
            joblog.LastChequeNumber = lastchequenumber;
            joblog.CustomerId = accountnumber;
            joblog.ChequeTypeId = chequetype;
            joblog.Bank = bank;
            joblog.Branch = branch;
            joblog.NumberBooks = numberbooks;
            joblog.Filename = "Printed from Database";
            joblog.CustomerName = customername;
            joblog.PrintUser = WindowsIdentity.GetCurrent().Name.ToString();
            joblog.NumberCheques = numbercheques;
            joblog.IsReprint = isreprint;

            manager.AddJobLog( joblog );
        }
    }
}
